const Service = require('egg').Service;

const sequelizeUtil = require('../lib/sequelizeUtil')

class AppService extends Service {

    async page(query) {
        const { app: { model: { Visit, VisitFee, Customer, User, Contact } } } = this

        sequelizeUtil.parsePageParam(query)
        sequelizeUtil.filterEmptyQuery(query.visit_where)
        sequelizeUtil.filterEmptyQuery(query.customer_where)
        sequelizeUtil.filterEmptyQuery(query.user_where)

        let ret = await Visit.findAndCountAll({
            offset: query.offset,
            limit: query.limit,
            order: query.order,
            include: [
                { model: Customer, require: true, attributes: ['name', 'dept'], where: query.customer_where, order: [['created_at', 'desc']] },
                { model: User, require: false, attributes: ['name'], where: query.user_where, },
                { model: VisitFee, require: false },
                { model: Contact, require: false },
            ],
            where: query.visit_where,
        })
        return ret
    }

}

module.exports = AppService 